package com.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.shop.bean.Credit;
import com.shop.dao.ICreditDao;
import com.shop.util.ConnDB;

/** 
 * @className: CreditDaoImpl.java
 * @classDescription: 
 * @function: 
 * @author: Victor
 * @createTime: 2012-11-1 下午07:16:26
 * @modifyTime: 
 * @modifyReason: 
 * @since: JDK 1.6
 */
public class CreditDaoImpl implements ICreditDao{
		
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private String sql = "";
	private int affectedLine = 0;
	private Credit credit = null;
	
	public int createCredit(Credit credit) throws Exception {
		Long creditId = credit.getCreditId();
		Long userId = credit.getUserId();
		float Credit = credit.getCredit();
		sql = "insert into address(creditId, userId, Credit) values(?, ?, ?)";
			
		conn = ConnDB.getConn();
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, creditId);
		pstmt.setLong(2, userId);
		pstmt.setFloat(3, Credit);
		
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
		}
	
	public int deleteCreditId(long creditId) throws Exception {
		conn = ConnDB.getConn();
		sql = "delete from address where creditId = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, creditId);
		affectedLine = pstmt.executeUpdate();
	
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
		}
	
	public List<Credit> queryAllCredit() throws Exception {
		sql = "select * from credit";
		pstmt = conn.prepareStatement(sql);
		List<Credit> creditList = new ArrayList<Credit>();
		rs = pstmt.executeQuery();
		Long CreditId = 0L;
		Long userId = 0L;
		float Credit = 0.0f;
		
		while(rs.next()){
			credit = new Credit();
			CreditId = rs.getLong("CreditId");
			credit.setCreditId(CreditId);
			userId = rs.getLong("userId");
			credit.setUserId(userId);
			Credit = rs.getInt("credit");
			credit.setCredit(Credit);
		}
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return creditList;
		}
	
	public List<Credit> queryAllCredit(int startPage, int pageSize)
	throws Exception {
		sql = "select * from address limit(? - 1) * ?, ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, startPage);
		pstmt.setInt(2, pageSize);
		pstmt.setInt(3, pageSize);
		List<Credit> creditList = new ArrayList<Credit>();
		rs = pstmt.executeQuery();
		Long CreditId = 0L;
		Long userId = 0L;
		float Credit = 0.0f;
		
		while(rs.next()){
			credit = new Credit();
			CreditId = rs.getLong("CreditId");
			credit.setCreditId(CreditId);
			userId = rs.getLong("userId");
			credit.setUserId(userId);
			Credit = rs.getInt("credit");
			credit.setCredit(Credit);
		}
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return creditList;
		}
	
	public int updateCredit(Credit credit) throws Exception {
		Long creditId = credit.getCreditId();
		Long userId = credit.getUserId();
		float Credit = credit.getCredit();
		
		sql = "update credit set creditId = ?, userId = ? where credit = ?";
		
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, creditId);
		pstmt.setLong(2, userId);
		pstmt.setFloat(3, Credit);
	
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
	}
	
	public Credit queryCreditId(long creditId) throws Exception {
		sql = "select * from address where creditId = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, creditId);
		rs = pstmt.executeQuery();
		
		Long CreditId = 0L;
		Long userId = 0L;
		float Credit = 0.0f;
		
		while(rs.next()){
			credit = new Credit();
			CreditId = rs.getLong("CreditId");
			credit.setCreditId(CreditId);
			userId = rs.getLong("userId");
			credit.setUserId(userId);
			Credit = rs.getInt("credit");
			credit.setCredit(Credit);
		}
		
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return credit;
	}
}
